package com.xianbeiying.app.ckgl.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.EsSubscBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.PhoneUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;

/**
 * 专家预约
 */
public class EsSubscribeFragment extends BaseFragment  implements HttpCallBackUtil{
    private View view;
    private EditText et_es_name,et_es_phone,et_trouble_content;
    private Button es_zjyl_btn;
    private ScrollView sc_essubsc;
    private static EsSubscribeFragment essFragment;
    public static EsSubscribeFragment getInstance(){
        essFragment=new EsSubscribeFragment();
        return essFragment;
    }
    private SetModel setModel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_es_zjyl,null);
        initView();
        return view;
    }
    private void initView() {
        setModel=new SetModel(getActivity());
        setModel.addResponseListener(this);
        sc_essubsc=(ScrollView) view.findViewById(R.id.sc_essubsc);
        et_trouble_content=(EditText) view.findViewById(R.id.et_trouble_content);
        et_es_name=(EditText) view.findViewById(R.id.et_es_name);
        et_es_phone=(EditText) view.findViewById(R.id.et_es_phone);
        es_zjyl_btn=(Button) view.findViewById(R.id.es_zjyl_btn);
        sc_essubsc.smoothScrollBy(0,0);
        es_zjyl_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trouble_content=et_trouble_content.getText().toString().trim();
                String esylName=et_es_name.getText().toString().trim();
                String esylPhone=et_es_phone.getText().toString().trim();
                if(trouble_content.isEmpty()){
                    ToastUtil.showInfo(getActivity(),"请输入问题描述");
                    return;
                }
                if(esylName.isEmpty()){
                    ToastUtil.showInfo(getActivity(),"姓名不能为空");
                    return;
                }
                if(esylPhone.isEmpty()){
                    ToastUtil.showInfo(getActivity(),"手机号不能为空");
                    return;
                }
                if (!PhoneUtil.isPhone(esylPhone)){
                    ToastUtil.showInfo(getActivity(),"手机号格式不正确");
                    return;
                }
                //专家预约
                setModel.ADDExpertOrder("EXPERT_ORDER",trouble_content,esylName,esylPhone,true);
            }
        });
    }

    @Override
    public void onCallBackData(String url,Object content) {
         if(url.equals("专家预约")){
                if(content!=null){
                    EsSubscBean esSubscBean= JSON.parseObject(new String((byte[]) content), EsSubscBean.class);
                    if(esSubscBean.isR()){
                        ToastUtil.showInfo(getActivity(),"提交成功");
                        et_es_name.setText("");
                        et_es_phone.setText("");
                        et_trouble_content.setText("");
                        sc_essubsc.smoothScrollBy(0,0);

                    }else{
                        ToastUtil.showInfo(getActivity(),esSubscBean.getERR_MSG());

                    }

                }
         }
    }
}
